package com.longcai.piaojia.activity;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.adapter.GoodsInfoLikeAdapter;
import com.longcai.piaojia.conn.Conn;
import com.longcai.piaojia.conn.GoodsInfoGet;
import com.longcai.piaojia.conn.MineCollectionDeleteGet;
import com.longcai.piaojia.conn.MyCollectAddGet;
import com.longcai.piaojia.dialog.WarnningDialog;
import com.longcai.piaojia.fragment.FragmentMyCollection;
import com.longcai.piaojia.fragment.MineFragment;
import com.longcai.piaojia.fragment.NewHomeFragment;
import com.longcai.piaojia.fragment.ShowFragment;
import com.longcai.piaojia.util.MyToast;
import com.longcai.piaojia.util.UtilPermission;
import com.longcai.piaojia.widget.CacheWebView;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;
import com.zcx.helper.util.UtilApp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by Administrator on 2016/8/2.
 */
public class GoodsInfoActivity extends BaseActivity implements View.OnClickListener {

    private static String share_show_id;
    private static String share_titel;
    private static String share_time;
    private static String share_address;

    @BoundView(R.id.goods_info_rl)
    private RelativeLayout goods_info_rl;
    @BoundView(R.id.title_collect_iv)
    private ImageView title_collect_iv;
    @BoundView(R.id.goods_info_iv)
    private ImageView goods_info_iv;
    @BoundView(R.id.goods_info_title_tv)
    private TextView goods_info_title_tv;
    @BoundView(R.id.goods_info_time_tv)
    private TextView goods_info_time_tv;
    @BoundView(R.id.goods_info_address_tv)
    private TextView goods_info_address_tv;
    @BoundView(R.id.goods_info_address_route_rl)
    private RelativeLayout goods_info_address_route_rl;
    @BoundView(R.id.goods_info_is_seat_tv)
    private TextView goods_info_is_seat_tv;
    @BoundView(R.id.goods_info_undetermined_tv)
    private TextView goods_info_undetermined_tv;
    @BoundView(R.id.goods_info_book_tv)
    private TextView goods_info_book_tv;
    @BoundView(R.id.goods_info_price_tv)
    private TextView goods_info_price_tv;
//    @BoundView(R.id.goods_info_seat_pic_tv) private TextView goods_info_seat_pic_tv;

    @BoundView(R.id.goods_info_show_time_tv)
    private TextView goods_info_show_time_tv;
    @BoundView(R.id.goods_info_show_area_tv)
    private TextView goods_info_show_area_tv;
    @BoundView(R.id.goods_info_show_address_tv)
    private TextView goods_info_show_address_tv;
    @BoundView(R.id.goods_info_media_iv)
    private ImageView goods_info_media_iv;
    @BoundView(R.id.goods_info_show_cwv)
    private CacheWebView goods_info_show_cwv;

    @BoundView(R.id.goods_info_like_ll)
    private LinearLayout goods_info_like_ll;
    @BoundView(R.id.goods_info_rv)
    private RecyclerView goods_info_rv;
    private GoodsInfoLikeAdapter goodsInfoLikeAdapter;

    @BoundView(R.id.goods_info_bug_info_rl)
    private RelativeLayout goods_info_bug_info_rl;
    @BoundView(R.id.goods_info_net_evaluate_rl)
    private RelativeLayout goods_info_net_evaluate_rl;
    @BoundView(R.id.goods_info_net_evaluate_tv)
    private TextView goods_info_net_evaluate_tv;

    @BoundView(R.id.goods_info_bottom_custom_service_ll)
    private LinearLayout goods_info_bottom_custom_service_ll;
    @BoundView(R.id.goods_info_bottom_submit_tv)
    private TextView goods_info_bottom_submit_tv;
    private GoodsInfoGet.GoodsInfoEntity goodsInfo;
    private PopupWindow popupWindow;

    public static String delivery_way;

    private static String share_icon_url;

    private List<GoodsInfoGet.GoodsInfoLikeEntity> list = new ArrayList<>();

    private int width;

    private int buyType = 0;

    private boolean isCollection = false;

    private int index;
    private boolean isRefresh = false;
    private GoodsInfoGet goodsInfoGet = new GoodsInfoGet(new AsyCallBack<GoodsInfoGet.GoodsInfoEntity>() {

        @Override
        public void onSuccess(String toast, int type, GoodsInfoGet.GoodsInfoEntity goodsInfoEntity) throws Exception {
            super.onSuccess(toast, type, goodsInfoEntity);

            goodsInfo = goodsInfoEntity;

            if (goodsInfoEntity.collect.equals("1")) {

                title_collect_iv.setImageResource(R.mipmap.collection_sel);

            } else {

                title_collect_iv.setImageResource(R.mipmap.collection_no);

            }
            delivery_way = goodsInfoEntity.delivery_way;
            share_titel = goodsInfoEntity.title;
            share_time = "时间：" + goodsInfoEntity.start_time;
            share_address = "地点：" + goodsInfoEntity.place_name;

            goods_info_title_tv.setText(goodsInfoEntity.title);
            goods_info_time_tv.setText(goodsInfoEntity.start_time);
            goods_info_address_tv.setText(goodsInfoEntity.place_address);

//            buyType = Integer.parseInt(goodsInfoEntity.is_seat);

            switch (Integer.parseInt(goodsInfoEntity.is_seat)) {

                case 0:

                    goods_info_is_seat_tv.setVisibility(View.GONE);
                    goods_info_undetermined_tv.setVisibility(View.GONE);

                    break;

                case 1:

                    goods_info_is_seat_tv.setVisibility(View.VISIBLE);
                    goods_info_undetermined_tv.setVisibility(View.GONE);

                    break;

                case 2:

                    goods_info_is_seat_tv.setVisibility(View.GONE);
                    goods_info_undetermined_tv.setVisibility(View.VISIBLE);
                    break;

            }

            switch (Integer.parseInt(goodsInfoEntity.status)) {

                case 0:

                    goods_info_book_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.hint_color));
                    goods_info_book_tv.setBackgroundResource(R.drawable.item_mycollection_nostart_text_bg);
                    goods_info_book_tv.setText(R.string.goods_info_status_notstart_text);
//                    goods_info_bottom_submit_tv.setText("");
                    goods_info_bottom_submit_tv.setText(R.string.goods_info_status_notstart_text);
                    goods_info_bottom_submit_tv.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.hint_color));
                    goods_info_bottom_submit_tv.setClickable(false);
                    break;
                case 1:

                    goods_info_book_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.item_mycollection_money_text_color));
                    goods_info_book_tv.setBackgroundResource(R.drawable.item_mycollection_ordering_text_bg);
                    goods_info_book_tv.setText(R.string.goods_info_status_ordering_text);
//                    goods_info_bottom_submit_tv.setText("");
                    goods_info_bottom_submit_tv.setText(R.string.goods_info_register_title_text);
                    goods_info_bottom_submit_tv.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.theme_color));
                    goods_info_bottom_submit_tv.setClickable(true);
                    buyType = 2;
                    break;
                case 2:

                    goods_info_book_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.item_mycollection_sale_text_color));
                    goods_info_book_tv.setBackgroundResource(R.drawable.item_mycollection_sale_text_bg);
                    goods_info_book_tv.setText(R.string.goods_info_status_selling_text);
//                    goods_info_bottom_submit_tv.setText("");

                    if (Integer.parseInt(goodsInfoEntity.is_seat) == 0) {

                        goods_info_bottom_submit_tv.setText("立即购买");
                        buyType = 0;

                    } else {

                        goods_info_bottom_submit_tv.setText(R.string.goods_info_bottom_seat_buy_text);
                        buyType = 1;

                    }
                    goods_info_bottom_submit_tv.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.theme_color));
                    goods_info_bottom_submit_tv.setClickable(true);
                    break;
                case 3:

                    goods_info_book_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.hint_color));
                    goods_info_book_tv.setBackgroundResource(R.drawable.item_mycollection_nostart_text_bg);
                    goods_info_book_tv.setText(R.string.goods_info_status_sellout_text);
//                    goods_info_bottom_submit_tv.setText("");
                    goods_info_bottom_submit_tv.setText(R.string.goods_info_status_sellout_text);
                    goods_info_bottom_submit_tv.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.hint_color));
                    goods_info_bottom_submit_tv.setClickable(false);
                    break;

            }

            goods_info_price_tv.setText(goodsInfoEntity.price_low + getString(R.string.goods_info_symbols_text) + goodsInfoEntity.price_high + getString(R.string.goods_info_money_text));

            goods_info_show_time_tv.setText(goodsInfoEntity.start_time + getString(R.string.space) + getString(R.string.goods_info_symbols_text) + getString(R.string.space) + goodsInfoEntity.end_time);

            goods_info_show_area_tv.setText(goodsInfoEntity.place_name);

            goods_info_show_address_tv.setText(goodsInfoEntity.place_address);

            if (TextUtils.isEmpty(goodsInfoEntity.media)) {

                goods_info_media_iv.setVisibility(View.GONE);

            } else {

                goods_info_media_iv.setVisibility(View.VISIBLE);
//                goods_info_media_iv.setImageBitmap(createVideoThumbnail(Conn.IMAGE_BANNER_URL + goodsInfoEntity.media, width, ScaleScreenHelperFactory.getInstance().getWidthHeight(426)));

            }

            goods_info_show_cwv.loadUrl(Conn.SERVICE + Conn.SHOW_WEB_VIEW + "?id=" + goodsInfoEntity.id);

            Log.e("------------>", Conn.SERVICE + Conn.SHOW_WEB_VIEW + "?id=" + goodsInfoEntity.id);

            goods_info_net_evaluate_tv.setText("（" + goodsInfoEntity.comment_number + "）");

            GlideLoader.getInstance().get(context, Conn.IMAGE_BANNER_URL + goodsInfoEntity.picurl, goods_info_iv, R.mipmap.goods);

            share_icon_url = Conn.IMAGE_BANNER_URL + goodsInfoEntity.picurl;

            list.addAll(goodsInfoEntity.guessYouLike);

            if (list.size() > 0) {

                goods_info_like_ll.setVisibility(View.VISIBLE);
                goodsInfoLikeAdapter.notifyDataSetChanged();

            } else {

                goods_info_like_ll.setVisibility(View.GONE);

            }

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

//            Toast.makeText(GoodsInfoActivity.this, toast, Toast.LENGTH_LONG).show();

        }

    });

    //视频第一帧图片
//    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
//    private Bitmap createVideoThumbnail(String url, int width, int height) {
//        Bitmap bitmap = null;
//        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
//        int kind = MediaStore.Video.Thumbnails.MINI_KIND;
//        try {
//            if (Build.VERSION.SDK_INT >= 14) {
//                retriever.setDataSource(url, new HashMap<String, String>());
//            } else {
//                retriever.setDataSource(url);
//            }
//            bitmap = retriever.getFrameAtTime();
//        } catch (IllegalArgumentException ex) {
//            // Assume this is a corrupt video file
//        } catch (RuntimeException ex) {
//            // Assume this is a corrupt video file.
//        } finally {
//            try {
//                retriever.release();
//            } catch (RuntimeException ex) {
//                // Ignore failures while cleaning up.
//            }
//        }
//        if (kind == MediaStore.Images.Thumbnails.MICRO_KIND && bitmap != null) {
//            bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
//                    ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
//        }
//        return bitmap;
//    }

    private MyCollectAddGet myCollectAddGet = new MyCollectAddGet(new AsyCallBack<String>() {

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {
            super.onSuccess(toast, type, s);

            goodsInfo.collect = "1";

            title_collect_iv.setImageResource(R.mipmap.collection_sel);

            if (isCollection) {

                FragmentMyCollection.RefreshCollectionlist refreshCollectionlist = (FragmentMyCollection.RefreshCollectionlist) getAppCallBack(FragmentMyCollection.class);
                refreshCollectionlist.refresh();

            }

            Toast.makeText(GoodsInfoActivity.this, toast, Toast.LENGTH_LONG).show();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(GoodsInfoActivity.this, toast, Toast.LENGTH_LONG).show();

        }

    });

    private MineCollectionDeleteGet mineCollectionDeleteGet = new MineCollectionDeleteGet(new AsyCallBack<String>() {

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {

            goodsInfo.collect = "0";

            title_collect_iv.setImageResource(R.mipmap.collection_no);

            if (isCollection) {

                FragmentMyCollection.RefreshCollectionlist refreshCollectionlist = (FragmentMyCollection.RefreshCollectionlist) getAppCallBack(FragmentMyCollection.class);
                refreshCollectionlist.refresh();

            }


            MyToast.show(GoodsInfoActivity.this, toast, Toast.LENGTH_LONG);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.show(GoodsInfoActivity.this, toast, Toast.LENGTH_LONG);

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_goods_info);

        String[] permission = {Manifest.permission.CALL_PHONE};
        UtilPermission.Permission(this, permission);

        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        width = metric.widthPixels;  // 屏幕宽度（像素）

        setBackTrue();

        setTitleName(getResources().getString(R.string.goods_info_title_text));

        setRightImage(this);


        registerReceiver(mHomeKeyEventReceiver, new IntentFilter(
                Intent.ACTION_CLOSE_SYSTEM_DIALOGS));

//        购买须知, 选座购买 隐藏，以后要显示
//        goods_info_net_evaluate_rl.setVisibility(View.GONE);
//        goods_info_bug_info_rl.setVisibility(View.GONE);
//        goods_info_bottom_submit_tv.setVisibility(View.GONE);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        goods_info_rv.setLayoutManager(linearLayoutManager);

        goodsInfoLikeAdapter = new GoodsInfoLikeAdapter(this, list);
        goods_info_rv.setAdapter(goodsInfoLikeAdapter);

        goodsInfoLikeAdapter.setOnItemClickListener(new GoodsInfoLikeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int pos) {

                Log.v("maning", "asdasd");

                if (list.get(pos).skiptype.equals("0")) {

                    Intent intent = new Intent();
                    //   intent.putExtra("delivery_way",goodsInfo.delivery_way);


                    intent.putExtra("id", list.get(pos).id);
                    intent.setClass(GoodsInfoActivity.this, GoodsInfoActivity.class);
                    startActivity(intent);

                    //  startActivity(new Intent(GoodsInfoActivity.this, GoodsInfoActivity.class).putExtra("id", list.get(pos).id));

                } else {

                    Bundle bundle = new Bundle();

                    bundle.putString("title", list.get(pos).title);
                    bundle.putString("url", Conn.SERVICE + Conn.ACTIVE_WEB + "?id=" + list.get(pos).id);

                    startActivity(new Intent(GoodsInfoActivity.this, WebActivity.class).putExtras(bundle));

                }

            }
        });

        goods_info_net_evaluate_rl.setOnClickListener(this);
        goods_info_bottom_custom_service_ll.setOnClickListener(this);
        goods_info_bottom_submit_tv.setOnClickListener(this);
        goods_info_address_route_rl.setOnClickListener(this);
        goods_info_bug_info_rl.setOnClickListener(this);
        goods_info_media_iv.setOnClickListener(this);


        isCollection = getIntent().getBooleanExtra("isCollection", false);

        setAppCallBack(new RefreshGoodsInfo());

        share_show_id = getIntent().getStringExtra("id");

        goodsInfoGet.id = getIntent().getStringExtra("id");
        goodsInfoGet.user_id = BaseApplication.BasePreferences.readUserId();
//        goodsInfoGet.region_id =getIntent().getStringExtra("region_id");


        goodsInfoGet.region_id =BaseApplication.BasePreferences.getCityId();


        goodsInfoGet.execute(this, false);


//         Toast.makeText(context,BaseApplication.BasePreferences.getCityId(), Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.title_collect_ll:

                if (!BaseApplication.BasePreferences.readIsLogin()) {

                    startActivity(new Intent(GoodsInfoActivity.this, LoginActivity.class));

                } else {

                    if (goodsInfo.collect.equals("1")) {

                        mineCollectionDeleteGet.user_id = BaseApplication.BasePreferences.readUserId();
                        mineCollectionDeleteGet.aid = goodsInfo.id;
                        mineCollectionDeleteGet.type = "1";
                        mineCollectionDeleteGet.execute(GoodsInfoActivity.this);

                    } else {

                        myCollectAddGet.user_id = BaseApplication.BasePreferences.readUserId();
                        myCollectAddGet.aid = goodsInfo.id;
                        myCollectAddGet.execute(GoodsInfoActivity.this);

                    }

                }

                break;

            case R.id.title_share_ll:

                if (popupWindow == null) {
                    popupWindow = new PopupWindow(GoodsInfoActivity.this);
                    popupWindow.setContentView(initView());
                    // PopupWindow必须设置高度和宽度
                    popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.popwindow_bg));
                    popupWindow.setWidth(LinearLayout.LayoutParams.MATCH_PARENT); // 设置宽度
                    popupWindow.setHeight(LinearLayout.LayoutParams.MATCH_PARENT); // 设置popWin 高度
                    popupWindow.showAtLocation(goods_info_rl, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
                    popupWindow.setOutsideTouchable(true);// 设置后点击其他位置PopupWindow将消失
                } else if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                } else {
                    popupWindow.showAtLocation(goods_info_rl, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
                }

                break;

            case R.id.goods_info_address_route_rl:

                startActivity(new Intent(GoodsInfoActivity.this, MapActivity.class).putExtra("goodsInfo", goodsInfo));

                break;

            case R.id.goods_info_bug_info_rl:

                Bundle bundleWeb = new Bundle();
                bundleWeb.putString("title", getResources().getString(R.string.goods_info_bug_info_title_text));
                bundleWeb.putString("url", Conn.SERVICE + Conn.ABOUT_PURCHASE);

                startActivity(new Intent(GoodsInfoActivity.this, WebActivity.class).putExtras(bundleWeb));

                break;

            case R.id.goods_info_net_evaluate_rl:

                startActivity(new Intent(GoodsInfoActivity.this, GoodsEvaluateListActivity.class).putExtra("goodsInfo", goodsInfo));

                break;

            case R.id.goods_info_bottom_custom_service_ll:

                new WarnningDialog(this, BaseApplication.BasePreferences.readCustomServiceTel(), getString(R.string.cancel_text), getString(R.string.confirm_text)) {
                    @Override
                    protected void onConfirm() {

                        UtilApp.call(GoodsInfoActivity.this, BaseApplication.BasePreferences.readCustomServiceTel());

                    }
                }.show();


                break;

            case R.id.goods_info_bottom_submit_tv:

                final Bundle bundle = new Bundle();

                switch (buyType) {

                    case 0:

                        if (!BaseApplication.BasePreferences.readIsLogin()) {

                            startActivity(new Intent(GoodsInfoActivity.this, LoginActivity.class).putExtra("isAfterGoodsInfo", true));

                            return;

                        }

                        bundle.putString("goodsInfoId", goodsInfo.id);
                        bundle.putString("picurl", goodsInfo.picurl);
                        bundle.putString("place_name", goodsInfo.place_name);
                        bundle.putString("place_address", goodsInfo.place_address);
                        bundle.putString("start_time", goodsInfo.start_time);
                        bundle.putString("title", goodsInfo.title);
                        bundle.putString("classid", goodsInfo.classid);

                        bundle.putString("is_seat", goodsInfo.is_seat);
                        bundle.putString("status", goodsInfo.status);

                        //  bundle.putString("delivery_way",goodsInfo.delivery_way);


                        startActivity(new Intent(GoodsInfoActivity.this, ChooseGoodsActivity.class).putExtras(bundle));

                        break;

                    case 1:

                        if (!BaseApplication.BasePreferences.readIsLogin()) {

                            startActivity(new Intent(GoodsInfoActivity.this, LoginActivity.class).putExtra("isAfterGoodsInfo", true));

                            return;

                        }

                        bundle.putString("goodsInfoId", goodsInfo.id);
                        bundle.putString("picurl", goodsInfo.picurl);
                        bundle.putString("place_name", goodsInfo.place_name);
                        bundle.putString("place_address", goodsInfo.place_address);
                        bundle.putString("start_time", goodsInfo.start_time);
                        bundle.putString("title", goodsInfo.title);
                        bundle.putString("classid", goodsInfo.classid);

                        bundle.putString("is_seat", goodsInfo.is_seat);
                        bundle.putString("status", goodsInfo.status);
                        // bundle.putString("delivery_way",goodsInfo.delivery_way);
//                        bundle.putString("productid", goodsInfo.productid);

                        //2.27日取消

//                        // 当选儿童剧时候显示  这个活动是到30号  到时候 把对话框取消
//                        if (goodsInfo.classid.equals("4")) {
//
//                            new HomeShowDialog(context, R.mipmap.choose_seat_dialog_child_iv) {
//                                @Override
//                                public void confirm() {
//
//                                    startActivity(new Intent(GoodsInfoActivity.this, ChooseGoodsActivity.class).putExtras(bundle));
//
//                                }
//                            }.show();
//
//                        } else {
//
//                            new HomeShowDialog(context, R.mipmap.choose_seat_dialog_iv) {
//                                @Override
//                                public void confirm() {
//
//                                    startActivity(new Intent(GoodsInfoActivity.this, ChooseGoodsActivity.class).putExtras(bundle));
//
//                                }
//                            }.show();
//
//                        }

                        startActivity(new Intent(GoodsInfoActivity.this, ChooseGoodsActivity.class).putExtras(bundle));


//                        trilateralLoginGet.productId = goodsInfo.productid;
//                        trilateralLoginGet.execute(GoodsInfoActivity.this);

                        break;

                    case 2:

//                        new RegisterDialog(GoodsInfoActivity.this, width, goodsInfo.title) {
//                            @Override
//                            protected void onSubmit(String phone) {
//
//
//
//                            }
//                        }.show();

                        if (!BaseApplication.BasePreferences.readIsLogin()) {

                            startActivity(new Intent(GoodsInfoActivity.this, LoginActivity.class).putExtra("isAfterGoodsInfo", true));

                            return;

                        }

                        bundle.putString("goodsInfoId", goodsInfo.id);
                        bundle.putString("picurl", goodsInfo.picurl);
                        bundle.putString("place_address", goodsInfo.place_address);
                        bundle.putString("place_name", goodsInfo.place_name);
                        bundle.putString("start_time", goodsInfo.start_time);
                        bundle.putString("title", goodsInfo.title);
                        bundle.putString("classid", goodsInfo.classid);

                        bundle.putString("is_seat", goodsInfo.is_seat);
                        bundle.putString("status", goodsInfo.status);
//                        bundle.putString("productid", goodsInfo.productid);

                        //  bundle.putString("delivery_way",goodsInfo.delivery_way);
                        startActivity(new Intent(GoodsInfoActivity.this, ChooseGoodsActivity.class).putExtras(bundle));

                        break;

                }

                break;

            case R.id.goods_info_media_iv:

                Intent it = new Intent(Intent.ACTION_VIEW);
                Uri uri = Uri.parse(Conn.IMAGE_BANNER_URL + goodsInfo.media);
                it.setDataAndType(uri, "video/mp4");
                startActivity(it);

                break;

        }

    }


    public class RefreshGoodsInfo implements AppCallBack {

        public void refresh() {

            goodsInfoGet.id = getIntent().getStringExtra("id");
            goodsInfoGet.user_id = BaseApplication.BasePreferences.readUserId();
            goodsInfoGet.execute(GoodsInfoActivity.this);

        }


    }

    private View initView() {

        View view = View.inflate(GoodsInfoActivity.this, R.layout.popwindow_my_setting_share, null);
        ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) view);
        RelativeLayout popwindow_main_rl = (RelativeLayout) view.findViewById(R.id.popwindow_main_rl);
        RelativeLayout my_setting_share_friend_rl = (RelativeLayout) view.findViewById(R.id.my_setting_share_friend_rl);
        RelativeLayout my_setting_share_pengyouquan_rl = (RelativeLayout) view.findViewById(R.id.my_setting_share_pengyouquan_rl);

        my_setting_share_friend_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                popupWindow.dismiss();
                showShare(GoodsInfoActivity.this, 1);

            }
        });

        my_setting_share_pengyouquan_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                popupWindow.dismiss();
                showShare(GoodsInfoActivity.this, 2);

            }
        });

        popwindow_main_rl.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                popupWindow.dismiss();

                return false;
            }
        });
        return view;

    }

    public static void showShare(final Context context, int type) {

//        Resources res = context.getResources();
//        Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.share_icon);


        if (type == 1) {

            final Wechat.ShareParams shareParams = new Wechat.ShareParams();
            shareParams.shareType = Platform.SHARE_WEBPAGE;

//            shareParams.setText("友票App 主要经营各类演出/体育赛事/景区景点门票及优惠券卡等，并开展本地生活类周边商家及商品的优选上线，为广大用户提供实惠及便捷的消费和购物体验");
            shareParams.setText(share_time + "\n" + share_address);

            shareParams.setTitle(share_titel);

//            Glide.with(context).load(share_icon_url).asBitmap().into(new SimpleTarget<Bitmap>() {
//                @Override
//                public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
//
//                    shareParams.setImageData(ThumbnailUtils.extractThumbnail(bitmap, 180, 180));
//
//                }
//            });

            shareParams.setImageUrl(share_icon_url);

            shareParams.setUrl(Conn.SERVICE + Conn.INDEX_SHARE_WEB + "?id=" + share_show_id);

            Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
            wechat.setPlatformActionListener(new PlatformActionListener() {
                @Override
                public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {

//                    UtilToast.show(context, context.getString(R.string.goods_info_share_success_text));

                }

                @Override
                public void onError(Platform platform, int i, Throwable throwable) {

//                    UtilToast.show(context, context.getString(R.string.goods_info_share_failed_text));

                }

                @Override
                public void onCancel(Platform platform, int i) {

//                    UtilToast.show(context, context.getString(R.string.goods_info_share_failed_text));

                }
            });

            wechat.share(shareParams);

        } else {

            final WechatMoments.ShareParams shareParams = new WechatMoments.ShareParams();
            shareParams.shareType = Platform.SHARE_WEBPAGE;
//            shareParams.setText("友票App 主要经营各类演出/体育赛事/景区景点门票及优惠券卡等，并开展本地生活类周边商家及商品的优选上线，为广大用户提供实惠及便捷的消费和购物体验");
            shareParams.setText(share_time + "\n" + share_address);

            shareParams.setTitle(share_titel);

//            Glide.with(context).load(share_icon_url).asBitmap().into(new SimpleTarget<Bitmap>() {
//                @Override
//                public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
//
//                    shareParams.setImageData(ThumbnailUtils.extractThumbnail(bitmap, 180, 180));
//
//                }
//            });

            shareParams.setImageUrl(share_icon_url);

            shareParams.setUrl(Conn.SERVICE + Conn.INDEX_SHARE_WEB + "?id=" + share_show_id);

            Platform wechatmoents = ShareSDK.getPlatform(WechatMoments.NAME);
            wechatmoents.setPlatformActionListener(new PlatformActionListener() {
                @Override
                public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {

//                    UtilToast.show(context, context.getString(R.string.goods_info_share_success_text));

                }

                @Override
                public void onError(Platform platform, int i, Throwable throwable) {

//                    UtilToast.show(context, context.getString(R.string.goods_info_share_failed_text));

                }

                @Override
                public void onCancel(Platform platform, int i) {

//                    UtilToast.show(context, context.getString(R.string.goods_info_share_failed_text));

                }
            });

            wechatmoents.share(shareParams);

        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {

            if (popupWindow != null) {

                if (popupWindow.isShowing()) {

                    popupWindow.dismiss();

                } else {

                    finish();

                }

            } else {

                finish();

            }

            return true;

        }

        return super.onKeyDown(keyCode, event);
    }


    private BroadcastReceiver mHomeKeyEventReceiver = new BroadcastReceiver() {
        String SYSTEM_REASON = "reason";
        String SYSTEM_HOME_KEY = "homekey";
        String SYSTEM_HOME_KEY_LONG = "recentapps";

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
                String reason = intent.getStringExtra(SYSTEM_REASON);
                if (TextUtils.equals(reason, SYSTEM_HOME_KEY)) {
                    //表示按了home键,程序到了后台
                    Log.v("maning", "进入后台");

                    isRefresh = true;

                } else if (TextUtils.equals(reason, SYSTEM_HOME_KEY_LONG)) {
                    //表示长按home键,显示最近使用的程序列表
                }
            }
        }
    };


    @Override
    protected void onResume() {
        super.onResume();

        if (isRefresh) {

            isRefresh = false;

            switch (index) {

                case 0:

                    Log.v("maning", "home进入前台");

                    try {

                        NewHomeFragment.RefreshNewHome refreshNewHome = (NewHomeFragment.RefreshNewHome) getAppCallBack(NewHomeFragment.class);
                        refreshNewHome.refresh();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    break;

                case 1:

                    Log.v("maning", "show进入前台");

                    try {

                        ShowFragment.setCityNameIdCallback setCityNameIdCallback = (ShowFragment.setCityNameIdCallback) getAppCallBack(ShowFragment.class);

                        setCityNameIdCallback.refresh();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                    break;

                case 4:

                    Log.v("maning", "mine进入前台");

                    try {

                        MineFragment.getUserAdvert getUserAdvert = (MineFragment.getUserAdvert) getAppCallBack(MineFragment.class);
                        getUserAdvert.refreshInfo();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                    break;

            }

        }

    }
}
